<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("comctrls.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Create_Trouble extends Page
{
       public $hfError = null;
       public $lblStart = null;
       public $Label2 = null;
       public $JSDecommission = null;
       public $hfDecommission = null;
       public $btnShowType = null;
       public $JTJavaScript1 = null;
       public $hiddenId = null;
       public $cmbServicedBy = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $dpStart = null;
       public $dpEnd = null;
       public $cmbRemedy = null;
       public $cmbTrouble = null;
       public $Label1 = null;
       public $Label86 = null;
       public $Label85 = null;
       public $Label93 = null;
       public $Image1 = null;
       function btnShowTypeJSClick($sender, $params)
       {

       ?>
       var win_height = 470;
       var win_width = 756;
       var height = window.opener.outerHeight;
       var width = window.opener.outerWidth;
       var top = Math.round((height-win_height)/2);
       var left = Math.round((width-win_width)/2);
       window.open("mini_trouble_remedy_droplist.php", 'Edit_Access_Level', 'location=no,height='+win_height+',width='+win_width+',top='+top+',left='+left);
       <?php

       }

       function cmbTroubleJSChange($sender, $params)
       {

       ?>
       var trouble = document.getElementById('cmbTrouble').value;
       troubleRemedy();
       return false;
       <?php

       }

       function btnCancelJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       window.close();
       <?php

       }


       function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->lblStart->Caption = '&darr; This field is required &darr;';
             $this->hfError->setValue('1');
          }
       }



       function Create_TroubleCreate($sender, $params)
       {
       $veh_id = $_GET['veh_id'];
       $this->hfError->setValue('0');

       if($veh_id!='' || $veh_id!=null)
       $this->hiddenId->setValue($veh_id);

       //DECOMMISSION
       $query_dec = "SELECT out_of_service FROM vehicle_tbl WHERE vehicle_id='".$this->hiddenId->getValue()."'";
       $result_dec = mysql_query($query_dec) or die("SELECT DEC ".mysql_error());
       $row_dec = mysql_fetch_array($result_dec);
       $this->hfDecommission->setValue($row_dec['out_of_service']);

       $this->dpStart->Text = date('Y-m-d');
       $this->dpEnd->Text = '';
       $this->lblStart->Caption='';

       /*$this->cmbServicedBy->Clear();
       $this->cmbServicedBy->AddItem('--Select--',null,null);
       $this->cmbServicedBy->setItemIndex('--Select--');
       $result_ServiceProvider = mysql_query("select * from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND category='serviced by'");

       while($row_ServiceProvider = mysql_fetch_array($result_ServiceProvider))
        {
            $this->cmbServicedBy->AddItem($row_ServiceProvider['value'], null, $row_ServiceProvider['value']);
        }*/

       $this->cmbServicedBy->Clear();
       $result_vendor = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

       while($row_vendor = mysql_fetch_array($result_vendor))
       {
            $this->cmbServicedBy->AddItem('--Select--', null, '--Select--');
            $this->cmbServicedBy->AddItem($row_vendor['vendor_name'], null, $row_vendor['vendor_name']);
       }

       $this->cmbTrouble->Clear();
       $this->cmbTrouble->AddItem('--Select--',null,null);
       $this->cmbTrouble->setItemIndex('--Select--');
       $result_Trouble = mysql_query("select * from trouble_droplist_tbl where company_id = '".$_SESSION['company_id']."'");

       while($row_Trouble = mysql_fetch_array($result_Trouble))
        {
            $this->cmbTrouble->AddItem($row_Trouble['value'], null, $row_Trouble['autoid']);
        }


       }

       function btnSubmitClick($sender, $params)
       {

       $this->validateRequired($this->dpStart->Text, 'lblid');

       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {
       $trouble_id = $this->cmbTrouble->getItemIndex();
       $result_troub = mysql_query("select * from trouble_droplist_tbl where autoid = '".$trouble_id."'");
       $row_troub = mysql_fetch_array($result_troub);

       $result = mysql_query("insert into veh_trouble_tbl (comp_id,
       veh_id,
       start_date,
       end_date,
       trouble,
       trouble_name,
       remedy,
       serviced_by)

       values ('".$_SESSION['company_id']."',
       '".$this->hiddenId->getValue()."',
       '".$this->dpStart->Text."',
       '".$this->dpEnd->Text."',
       '".$this->cmbTrouble->getItemIndex()."',
       '".$row_troub['value']."',
       '".$this->cmbRemedy->getItemIndex()."',
       '".$this->cmbServicedBy->getItemIndex()."')")or die("INSERT trouble - " . mysql_error());

       /*
       echo "<script type=text/javascript>
        alert('Successfully added record!');
        window.opener.location.reload();
        window.close();
        </script>";
       */

       echo "<script type='text/javascript'>
       alert('Successfully added record!');

       //ADD ANOTHER RECORD?
       var answer = confirm('Add another record?');
       if(answer)
       {
       window.opener.location.reload();
       window.location = 'veh_trouble_create.php?veh=".$this->hiddenId->getValue()."';
       }
       else
       {

          //DECOMMISSION VEHICLE?
          if(".$this->hfDecommission->getValue()."=='0')
          {
          var decommission = confirm('Click OK to DECOMMISSION vehicle or CANCEL to continue?');
          if(decommission)
          {
             if (window.XMLHttpRequest)
             {
             xmlhttp = new XMLHttpRequest();
             }
             else if (window.ActiveXObject)
             {
             xmlhttp =  new ActiveXObject('Microsoft.XMLHTTP');
             }
             else
             {
             xmlhttp = null;
             }


             if (xmlhttp==null)
             {
             alert ('Your browser does not support AJAX!');
             }
             var veh_id = '".$this->hiddenId->getValue()."';
             var date = '".$this->dpStart->Text."';
             var trouble = '".$this->cmbTrouble->getItemIndex()."';
             var url='decommission.php';
             url=url+'?trouble='+trouble+'&veh_id='+veh_id+'&date='+date;
             xmlhttp.open('GET',url,true);
             xmlhttp.send(null);
          }
          }


          //CREATE WORKORDER?
          var workorder = confirm('Click OK to create a WORK ORDER or CANCEL to continue');
          if(workorder)
          {
          veh_id = '".$this->hiddenId->getValue()."';
          //workorder_open_create('".$this->hiddenId->getValue()."');
          window.opener.parent.workorder_open_create('".$this->hiddenId->getValue()."');

          //window.opener.parent.location.href = 'trouble_workorder_list.php?veh_id=".$this->hiddenId->getValue()."&layer=1';
          window.opener.location.reload();
          window.close();
          }

       window.opener.location.reload();
       window.close();
       }
       </script>";
       }



       }

}

global $application;

global $Create_Trouble;

//Creates the form
$Create_Trouble=new Create_Trouble($application);

//Read from resource file
$Create_Trouble->loadResource(__FILE__);

//Shows the form
$Create_Trouble->show();

?>